package com.ymatou.iapi.couponmanage.testcase;

import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.GetTaskInfoBean;
import com.ymatou.iapi.couponmanage.parameter.QuerySendTask;
import com.ymatou.iapi.couponmanage.parameter.TaskInfo;
import com.ymatou.iapi.couponmanage.service.GetTaskInfoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponGenerateTask;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponGenerateTaskWapper;
/**
*获取任务信息用例
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetTaskInfo{
	private static GetTaskInfoBean gettaskinfoBean;
	private static GetTaskInfoCall gettaskinfoCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取任务信息用例");
	}

	@Before
	public void caseUp() {
		gettaskinfoBean=new GetTaskInfoBean();
		gettaskinfoCall=new GetTaskInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(gettaskinfoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskInfo_001() {
		Logger.start(true,"正例-获取任务信息");
		try {
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectTop1().get(0);			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			gettaskinfoBean.setTaskId(task.getTaskId());
			
			gettaskinfoCall.setData(gettaskinfoBean);
			gettaskinfoCall.callService();			
			
			verifyReturn(task,batch);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskInfo_002() {
		Logger.start(true,"正例-获取任务信息-多子任务");
		try {
			Map result = new YmtCouponGenerateTaskWapper().selectMutlipySubTask().get(0);
			
			int taskid = Integer.parseInt(result.get("TaskId").toString());
			int num = Integer.parseInt(result.get("num").toString());
			
			YmtCouponGenerateTask task = new YmtCouponGenerateTaskWapper().selectByPrimaryKey(taskid);
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectByPrimaryKey(task.getCouponBatchId());

			gettaskinfoBean.setTaskId(taskid);
			
			gettaskinfoCall.setData(gettaskinfoBean);
			gettaskinfoCall.callService();			
			
			verifyReturn(task,batch);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskInfo_003() {
		Logger.start(true,"反例-taskid空");
		try {
			gettaskinfoBean.setTaskId(null);
			
			gettaskinfoCall.setData(gettaskinfoBean);
			gettaskinfoCall.callService();			
			
			Logger.verifyEquals("0", gettaskinfoCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", gettaskinfoCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(null, gettaskinfoCall.getTaskInfo(), "验证返回code");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetTaskInfo_004() {
		Logger.start(true,"反例-taskid不存在");
		try {
			gettaskinfoBean.setTaskId(9999999);
			
			gettaskinfoCall.setData(gettaskinfoBean);
			gettaskinfoCall.callService();			
			
			Logger.verifyEquals("0", gettaskinfoCall.getCode(), "验证返回code");
			Logger.verifyEquals("操作成功！", gettaskinfoCall.getMessage(), "验证返回Message");
			Logger.verifyEquals(null, gettaskinfoCall.getTaskInfo(), "验证返回code");

			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void verifyReturn(YmtCouponGenerateTask task, YmtCouponBatch batch)
	{
		Logger.verifyEquals("0", gettaskinfoCall.getCode(), "验证返回code");
		Logger.verifyEquals("操作成功！", gettaskinfoCall.getMessage(), "验证返回Message");

		TaskInfo info = gettaskinfoCall.getTaskInfo();
		
		Logger.verifyEquals(task.getTaskId(), info.getTaskId(), "验证返回code");
		Logger.verifyEquals(task.getCouponBatchId(), info.getBatchId(), "验证返回code");
		Logger.verifyEquals(task.getTaskCreaterId(), info.getCreaterId(), "验证返回code");
		Logger.verifyEquals(task.getTaskCreaterName(), info.getCreaterName(), "验证返回code");
		Logger.verifyEquals(task.getTaskStatus().intValue(), info.getTaskStatus(), "验证返回code");
		Logger.verifyEquals(task.getTaskHadGenerateCount(), info.getCouponSuccessCount(), "验证返回code");
		Logger.verifyEquals(task.getTaskFailGenerateCount(), info.getCouponFailCount(), "验证返回code");
		Logger.verifyEquals(task.getTaskGenerateCount(), info.getCouponTotal(), "验证返回code");
		Logger.verifyEquals(batch.getsBatchCode(), info.getBatchCode(), "验证返回code");
		Logger.verifyEquals(batch.getsCouponName(), info.getBatchName(), "验证返回code");
	}
}